package cn.edu.seig.textbook.controller;


import cn.edu.seig.textbook.domain.dto.OrderTextbook;
import cn.edu.seig.textbook.domain.pojo.Orders;
import cn.edu.seig.textbook.domain.pojo.Students;
import cn.edu.seig.textbook.domain.pojo.Textbooks;
import cn.edu.seig.textbook.domain.pojo.Users;
import cn.edu.seig.textbook.enums.OrderEnum;
import cn.edu.seig.textbook.service.IOrdersService;
import cn.edu.seig.textbook.service.IStudentsService;
import cn.edu.seig.textbook.service.ITextbooksService;
import cn.edu.seig.textbook.service.IUsersService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author day
 * @since 2024-11-07
 */
@Controller
@RequestMapping("/student")
public class StudentsController {
    @Autowired
    private ITextbooksService textbooksService;

    @Autowired
    private IStudentsService studentsService;
    @Autowired
    private IUsersService usersService;
    @Autowired
    private IOrdersService ordersService;


    @RequestMapping("/toStudent")
    public String toStudent(@AuthenticationPrincipal UserDetails userDetails,HttpSession session) {
        session.setAttribute("userDetails", userDetails);

        return "student/student";
    }

    @RequestMapping("/textbook_all")
    public String textbookAll(HttpServletRequest request, @AuthenticationPrincipal UserDetails userDetails) {

        Users user = usersService.getUser(userDetails.getUsername());
        Students student = studentsService.getStudentByUserId(user.getUserId());
        request.setAttribute("student", student);
        List<Textbooks> textbooks=textbooksService.getAllTestbooks();
        request.setAttribute("textbooks", textbooks);
        return "student/showAllTextbook";
    }
    @RequestMapping("/findBookName")
    public String findBookName(HttpServletRequest request,@Param("bookName") String bookName,@AuthenticationPrincipal UserDetails userDetails){
        Users user = usersService.getUser(userDetails.getUsername());
        Students student = studentsService.getStudentByUserId(user.getUserId());
        request.setAttribute("student", student);
        List<Textbooks> textbooks=textbooksService.getTextbooksByBookName(bookName);
        request.setAttribute("textbooks", textbooks);
        return "student/showAllTextbook";}

    @RequestMapping("/addOrders")
    public String addOrders(String studentId,@RequestParam("ids") int[] ids) {
        System.out.println(ids.length);
        for(int id:ids){
            Orders orders = new Orders();
            Textbooks textbooks = textbooksService.getTextbookById(id);
            orders.setTextbookId(textbooks.getTextbookId());
            orders.setStudentId(Integer.valueOf(studentId));
            orders.setOrderDate(new Date());
            orders.setStudentName(studentsService.getStudentById(studentId).getName());
            orders.setTextbookName(textbooks.getBookName());
            orders.setOrderStatus(OrderEnum.NOORDER);
            ordersService.addOrder(orders);

        }
       return "redirect:/student/textbook_all";
    }

    @RequestMapping("/orders")
    public String orders(Model model, @AuthenticationPrincipal UserDetails userDetails) {
        List<OrderTextbook> orderTextbooks=new ArrayList<OrderTextbook>();
        Users user = usersService.getUser(userDetails.getUsername());
        Students student = studentsService.getStudentByUserId(user.getUserId());
        List<Orders> orders=ordersService.getOrdersByStudentId(student.getStudentId());
        for (Orders order:orders){
            OrderTextbook orderTextbook=new OrderTextbook();
            Textbooks textbooks = textbooksService.getTextbookById(order.getTextbookId());
            orderTextbook.setOrder(order);
            orderTextbook.setTextbook(textbooks);
            orderTextbooks.add(orderTextbook);
        }
        model.addAttribute("orderTextbooks", orderTextbooks);
        return "student/showAllOrders";
    }
    @RequestMapping("/deleteOrder")
    public String deleteOrder(HttpServletRequest request,@RequestParam("orderId") int orderId,@RequestParam("orderStatus") String orderStatus) {

        if (Objects.equals(orderStatus, "已定购")) {
            request.setAttribute("msg","该书已订购，无法退定");
            return "redirect:/student/orders";
        }
        ordersService.deleteOrder(orderId);
        return "redirect:/student/orders";
    }

}
